package zhengyanze.com.aipoemmax.guide.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import zhengyanze.com.aipoemmax.R;
import zhengyanze.com.aipoemmax.guide.activity.base.BaseActivity;
import zhengyanze.com.aipoemmax.guide.adapter.MainAdapter;
import zhengyanze.com.aipoemmax.guide.bean.PoemBean;
import zhengyanze.com.aipoemmax.guide.utils.LogUtils;

public class SuiBiActicity extends BaseActivity {
    public static final String SUIBI_FLAG = "Mysuibi";
    @BindView(R.id.rv_suibi)
    RecyclerView rvSuibi;
    @BindView(R.id.has_data)
    TextView hasData;

    ArrayList<PoemBean> mPoembeans = new ArrayList<>();
    private MainAdapter mainAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sui_bi_acticity);
        ButterKnife.bind(this);
        initView();
        getData();
    }

    public void initView() {
        rvSuibi.setLayoutManager(new LinearLayoutManager(this));
        mainAdapter = new MainAdapter(mPoembeans);
        rvSuibi.setAdapter(mainAdapter);
    }


    /**
     * 获取数据
     */
    public void getData() {
        AVQuery<AVObject> avQuery = new AVQuery<>("SuiBi_Product");
        avQuery.orderByDescending("createdAt");
        avQuery.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if (e == null) {
                    mPoembeans.clear();
                    for (int i = 0; i < list.size(); i++) {
                        AVObject avObject = list.get(i);
                        String title = avObject.getString("title");
                        String author = avObject.getString("author");
                        String content = avObject.getString("content");

                        PoemBean poemBean = new PoemBean(title, author, content, SUIBI_FLAG);
                        mPoembeans.add(poemBean);
                    }
                    if (!mPoembeans.isEmpty()) {
                        hasData.setVisibility(View.GONE);
                    }else{
                        hasData.setVisibility(View.VISIBLE);
                    }
                    mainAdapter.notifyDataSetChanged();
                } else {
                    e.printStackTrace();
                }
            }
        });
    }

    @OnClick(R.id.add)
    public void onViewClicked() {
        AddSuibiActivity.setCallback(new backAddSuccess() {
            @Override
            public void updateUI(PoemBean poemBean) {
                mPoembeans.add(poemBean);
                mainAdapter.notifyDataSetChanged();
                hasData.setVisibility(View.GONE);
            }
        });
        startActivity(new Intent(this,AddSuibiActivity.class));
    }

    /**
     * 回调接口
     */
    interface backAddSuccess{
        void updateUI(PoemBean poemBean);
    }
}
